package com.zhouyong.netty.config;

import com.zhouyong.netty.handler.CustomizeServerHandler;
import com.zhouyong.protocol.CustomizeFrameDecoder;
import com.zhouyong.protocol.CustomizeProtocol;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.logging.LoggingHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author 99Savage
 * @date 2023/3/17 9:40
 */
@Component
public class ServerChannelInitializer extends ChannelInitializer<SocketChannel>{

    @Resource
    private CustomizeServerHandler serverHandler;

    @Resource
    private CustomizeProtocol protocol;

    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        // 自定义协议
        channel.pipeline().addLast(protocol);
        // LTC解码器解决粘包、拆包
        channel.pipeline().addLast(new CustomizeFrameDecoder());
        // 入站处理器
        channel.pipeline().addLast(serverHandler);
        // debug信息
        channel.pipeline().addLast(new LoggingHandler());
    }
}
